package kr.hyosang.android.penfo;

import java.net.MalformedURLException;

import kr.hyosang.HttpUtil;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.os.AsyncTask;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class HttpImage
{
    private ImageView view;
    private String url;
    public HttpUtil httpUtil;
    private ImageHttpTask imageTask;
    private ScaleType mScaleType;
    private int mLayoutScaleType;
    
    public final static int SCALETYPE_ADJUST_LAYOUT = 1;
    public final static int SCALETYPE_MAXIMUM_WIDTH = 2;
    
    public HttpImage(ImageView imageView, String u) {
        view = imageView;
        url = u;
        mLayoutScaleType = SCALETYPE_ADJUST_LAYOUT;
    }
    
    public void setLayoutScaleType(int t) {
        mLayoutScaleType = t;
    }
    
    public void load() {
    	mScaleType = view.getScaleType();
    	view.setScaleType(ScaleType.CENTER_INSIDE);
    	
    	//show loading image
    	view.setImageResource(R.drawable.loading);
    	((AnimationDrawable)view.getDrawable()).start();
    	
        imageTask = new ImageHttpTask();
        imageTask.execute(this);
    }
    
    public void setImage(Bitmap bm) {
        Context context = SharedObject.getInstance().mAppContext;
        
        WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        int nScreenWidth = wm.getDefaultDisplay().getWidth();
        
        int nImgWidth = bm.getWidth();
        int nImgHeight = bm.getHeight();
        
        LayoutParams layoutParam = view.getLayoutParams();
        
        if(mLayoutScaleType == SCALETYPE_ADJUST_LAYOUT) {
            if(layoutParam.width == -1) {
                layoutParam.width = nScreenWidth;
            }
        }else if(mLayoutScaleType == SCALETYPE_MAXIMUM_WIDTH) {
            layoutParam.width = (nImgWidth < nScreenWidth) ? nImgWidth : nScreenWidth;
        }
        
        layoutParam.height = (int)(((double)layoutParam.width/(double)nImgWidth) * nImgHeight);
        
        Log.d("penfo", "width = " + layoutParam.width + ", height = " + layoutParam.height);
        
        view.setLayoutParams(layoutParam);
        view.setImageBitmap(bm);
        
    }
    
    class ImageHttpTask extends AsyncTask<HttpImage, Void, HttpImage> {

        @Override
        protected HttpImage doInBackground(HttpImage... arg0)
        {
            try
            {
                httpUtil = new HttpUtil();
                httpUtil.setURL(arg0[0].url);
                
                httpUtil.process();
                
            } catch (MalformedURLException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            return null;
        }
        
        @Override
        protected void onPostExecute(HttpImage httpImage) {
        	Log.d("penfo", "ErrorMsg : " + httpUtil.getHttpErrorMessage());
        	view.setScaleType(mScaleType);
        	
            byte[] data = httpUtil.getResponseData();
            
            if(data != null) {
                Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
                setImage(bm);
            }else {
                view.setImageDrawable(null);
            }
        }        
    }

}
